package com.zte.cl.parser;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import com.zte.cl.model.News;
import com.zte.cl.parser.enums.PageType;
import com.zte.cl.service.NewsService;
import com.zte.cl.service.PageContentProcessService;
import com.zte.cl.utils.SpringContextUtil;
import com.zte.cl.utils.StringUtil;
import com.zte.cl.utils.UuidUtil;

/**
 * 种子页结果处理 Pipeline负责抽取结果的处理，包括计算、持久化到文件、数据库等。
 * 
 * @author Administrator
 * 
 */
public class SeedPagePipeline implements Pipeline {

	public final static Logger log = Logger.getLogger(SeedPagePipeline.class);

	private static String info(String channelId, String pageUrl, String content) {
		if (!StringUtil.isEmpty(pageUrl)) {
			return String.format("channelId=%s,pageUrl=%s", channelId, pageUrl);
		}
		if (!StringUtil.isEmpty(content)) {
			return String.format("channelId=%s,content=%s", channelId, content);
		}
		return null;
	}
	
	private PageContentProcessService pageContentProcessServiceSeedPageImpl = (PageContentProcessService) SpringContextUtil.getBean("pageContentProcessServiceSeedPageImpl");
	private PageContentProcessService pageContentProcessServiceContentPageImpl = (PageContentProcessService) SpringContextUtil.getBean("pageContentProcessServiceContentPageImpl");

	private NewsService newsService = (NewsService) SpringContextUtil.getBean("newsService");
	
	@SuppressWarnings("unchecked")
	@Override
	public void process(ResultItems resultItems, Task task) {
		Map<String, Object> mapResult = resultItems.getAll();
		String seedPageType = mapResult.get("seedPageType").toString();
		List<News> newsList = (List<News>) mapResult.get("newsList");
		
		if (newsList != null) {
			for (News news : newsList) {
				try {
					if (PageType.getEnumFromString(seedPageType) == PageType.textPage) {
						log.debug("段子类型,通过内容直接从数据库中查找是否已经存在");
						// 段子类型判断重复通过内容判断
						if (newsService.isExistsByContent(news.getChannelId(), news.getContent())) {
							log.debug(info(news.getChannelId(), news.getPageUrl(), news.getContent()) + "已存在");
							continue;
						} else {
							log.debug("数据库中都不存在, 需要处理此条news");
						}
					} else {
						// 通过channelId, pageUrl, title判断是否存在
						if (newsService.isExistsByUrlOrTitle(news.getChannelId(), news.getPageUrl(), news.getTitle())) {
							log.debug(info(news.getChannelId(), news.getPageUrl(), news.getContent()) + "已存在");
							continue;
						} else {
							log.debug("缓存和数据库中都不存在, 需要处理此条news");
						}
					}
					news.setId(UuidUtil.getShortUuid());
					if (PageType.getEnumFromString(seedPageType) == PageType.listPage
							|| (PageType.getEnumFromString(seedPageType) == PageType.rssPage && news.getPageUrl() != null && StringUtil
									.isEmpty(news.getContent()))) {
						// 需要通过url再次抓取，才能获得内容
						pageContentProcessServiceSeedPageImpl.processContent(news, null);
					} else {
						// 已经获得内容
						pageContentProcessServiceContentPageImpl.processContent(news, null);
					}
				} catch (Exception e) {
					log.error(e);
				}
			}
		}
	}
}
